<html>
<head><meta charset="utf-8"><title>Miri: no item to reborrow for Unique · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html">Miri: no item to reborrow for Unique</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="164998024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/164998024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#164998024">(May 06 2019 at 16:48)</a>:</h4>
<blockquote>
<p>Miri evaluation error: no item to reborrow for Unique from tag 3488 found in borrow stack</p>
</blockquote>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span>  is this a "programmer did something bad" error or "Miri doesn't know this" error? I'm thinking the former.</p>



<a name="165019391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165019391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165019391">(May 06 2019 at 21:11)</a>:</h4>
<p>yeah that's a "you got UB" error</p>



<a name="165034593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165034593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165034593">(May 07 2019 at 01:39)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span>  would you mind helping me see where?</p>
<div class="codehilite"><pre><span></span><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">collections</span>::<span class="n">BTreeSet</span><span class="p">;</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">uniq_refs</span><span class="o">&lt;</span><span class="na">&#39;i</span><span class="p">,</span><span class="w"> </span><span class="na">&#39;d</span>: <span class="na">&#39;i</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="n">data</span>: <span class="kp">&amp;</span><span class="na">&#39;d</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">],</span><span class="w"></span>
<span class="w">    </span><span class="n">indices</span>: <span class="kp">&amp;</span><span class="na">&#39;i</span><span class="w"> </span><span class="n">BTreeSet</span><span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">impl</span><span class="w"> </span><span class="nb">Iterator</span><span class="o">&lt;</span><span class="n">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="na">&#39;d</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="na">&#39;i</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">in_bounds_indices</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">indices</span><span class="p">.</span><span class="n">range</span><span class="p">(</span><span class="mi">0</span><span class="p">..</span><span class="n">data</span><span class="p">.</span><span class="n">len</span><span class="p">());</span><span class="w"></span>

<span class="w">    </span><span class="n">in_bounds_indices</span><span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="k">move</span><span class="w"> </span><span class="o">|&amp;</span><span class="n">i</span><span class="o">|</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">data</span><span class="p">.</span><span class="n">get_unchecked_mut</span><span class="p">(</span><span class="n">i</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">p</span>: <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">r</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="n">p</span><span class="w"></span>
<span class="w">    </span><span class="p">})</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">iter</span>::<span class="n">FromIterator</span><span class="p">;</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">scores</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">vec</span><span class="o">!</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">indices</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">BTreeSet</span>::<span class="n">from_iter</span><span class="p">(</span><span class="n">vec</span><span class="o">!</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">]);</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">selected_scores</span>: <span class="nb">Vec</span><span class="o">&lt;</span><span class="n">_</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">uniq_refs</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">scores</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">indices</span><span class="p">).</span><span class="n">collect</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="n">score</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">selected_scores</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">*</span><span class="n">score</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{:?}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">scores</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<div class="codehilite"><pre><span></span>error[E0080]: Miri evaluation error: no item to reborrow for Unique from tag 3488 found in borrow stack
    --&gt; /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/mem.rs:1328:34
     |
1328 |         ManuallyDrop::into_inner(self.value)
     |                                  ^^^^^^^^^^ Miri evaluation error: no item to reborrow for Unique from tag 3488 found in borrow stack
     |
     = note: inside call to `std::mem::MaybeUninit::&lt;&amp;mut i32&gt;::assume_init` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/ptr.rs:578:5
     = note: inside call to `std::ptr::read::&lt;&amp;mut i32&gt;` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/liballoc/vec.rs:2368:26
note: inside call to `&lt;std::vec::IntoIter&lt;&amp;mut i32&gt; as std::iter::Iterator&gt;::next` at src/main.rs:24:18
    --&gt; src/main.rs:24:18
     |
24   |     for score in selected_scores {
     |                  ^^^^^^^^^^^^^^^
     = note: inside call to `main` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:64:34
     = note: inside call to closure at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:52:53
     = note: inside call to closure at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/panicking.rs:293:40
     = note: inside call to `std::panicking::try::do_call::&lt;[closure@DefId(1/1:1542 ~ std[82ff]::rt[0]::lang_start_internal[0]::{{closure}}[0]) 0:&amp;dyn std::ops::Fn() -&gt; i32 + std::marker::Sync + std::panic::RefUnwindSafe], i32&gt;` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/panicking.rs:289:5
     = note: inside call to `std::panicking::try::&lt;i32, [closure@DefId(1/1:1542 ~ std[82ff]::rt[0]::lang_start_internal[0]::{{closure}}[0]) 0:&amp;dyn std::ops::Fn() -&gt; i32 + std::marker::Sync + std::panic::RefUnwindSafe]&gt;` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/panic.rs:388:9
     = note: inside call to `std::panic::catch_unwind::&lt;[closure@DefId(1/1:1542 ~ std[82ff]::rt[0]::lang_start_internal[0]::{{closure}}[0]) 0:&amp;dyn std::ops::Fn() -&gt; i32 + std::marker::Sync + std::panic::RefUnwindSafe], i32&gt;` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:52:25
     = note: inside call to `std::rt::lang_start_internal` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:64:5
     = note: inside call to `std::rt::lang_start::&lt;()&gt;`
</pre></div>


<p><a href="https://play.integer32.com/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=6bbb16e5beda1bfa7efa0c2e079dc1ba" target="_blank" title="https://play.integer32.com/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=6bbb16e5beda1bfa7efa0c2e079dc1ba">playground</a></p>



<a name="165034659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165034659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165034659">(May 07 2019 at 01:41)</a>:</h4>
<p>If I remove the <code>Vec</code> and iterate directly, Miri stays silent.</p>



<a name="165053144"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165053144" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165053144">(May 07 2019 at 08:43)</a>:</h4>
<p>(Sorry, I am on a workshop and don't have time to dig into this right now -- I'll come back to you eventually.)</p>



<a name="165065942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165065942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165065942">(May 07 2019 at 12:24)</a>:</h4>
<p>How very rude. /s <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>



<a name="165169468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165169468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165169468">(May 08 2019 at 14:49)</a>:</h4>
<p>uh, you are doing illegal lifetime laundering here</p>



<a name="165169472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165169472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165169472">(May 08 2019 at 14:49)</a>:</h4>
<p>casting <code>&amp;mut</code> to <code>*mut</code> and back</p>



<a name="165169538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165169538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165169538">(May 08 2019 at 14:50)</a>:</h4>
<p>and the reason it explodes is that every time you use <code>data</code>, you are asserting that this is a unique reference</p>



<a name="165169718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165169718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165169718">(May 08 2019 at 14:52)</a>:</h4>
<p>This is basically equivalent to</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">fst</span><span class="p">(</span><span class="n">x</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="p">(</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="kt">i32</span><span class="p">))</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="mi">0</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">snd</span><span class="p">(</span><span class="n">x</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="p">(</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="kt">i32</span><span class="p">))</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="mi">0</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">ptr1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fst</span><span class="p">(</span><span class="n">p</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">ptr1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="p">(</span><span class="n">ptr1</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"> </span><span class="c1">// lifetime laundering</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">ptr2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">snd</span><span class="p">(</span><span class="n">p</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">ptr1</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="165169742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165169742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165169742">(May 08 2019 at 14:52)</a>:</h4>
<p>Calling <code>snd</code> creates a mutable reference overlapping with <code>ptr1</code>, thus invalidating <code>ptr1</code></p>



<a name="165170116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170116">(May 08 2019 at 14:56)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> ^</p>



<a name="165170193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170193">(May 08 2019 at 14:57)</a>:</h4>
<p>You say <em>lifetime</em> laundering — is that precise?</p>



<a name="165170213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170213">(May 08 2019 at 14:57)</a>:</h4>
<p>Conceptually, I'm intending to do the same thing as e.g. <code>split_at_mut</code></p>



<a name="165170282"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170282" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170282">(May 08 2019 at 14:58)</a>:</h4>
<p>I want to get two disjoint mutable references to values in a slice.</p>



<a name="165170374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170374">(May 08 2019 at 14:59)</a>:</h4>
<p>you need to go through raw pointers then</p>



<a name="165170375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170375">(May 08 2019 at 14:59)</a>:</h4>
<p>Do I need to switch to getting a <code>*mut T</code> and use <code>add</code> instead of <code>get_unchecked_mut</code>?</p>



<a name="165170397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170397">(May 08 2019 at 14:59)</a>:</h4>
<p>and unfortunately we have no <code>get</code> on a <code>*mut [T]</code>, so you need to use ptr arithmetic</p>



<a name="165170474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170474">(May 08 2019 at 15:00)</a>:</h4>
<blockquote>
<p>Do I need to switch to getting a <code>*mut T</code> and use <code>add</code> instead of <code>get_unchecked_mut</code>?</p>
</blockquote>
<p>yes</p>



<a name="165170504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170504">(May 08 2019 at 15:00)</a>:</h4>
<p>OK</p>
<div class="codehilite"><pre><span></span><span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">start</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">data</span><span class="p">.</span><span class="n">as_mut_ptr</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">in_bounds_indices</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">indices</span><span class="p">.</span><span class="n">range</span><span class="p">(</span><span class="mi">0</span><span class="p">..</span><span class="n">data</span><span class="p">.</span><span class="n">len</span><span class="p">());</span><span class="w"></span>

<span class="w">    </span><span class="n">in_bounds_indices</span><span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="k">move</span><span class="w"> </span><span class="o">|&amp;</span><span class="n">i</span><span class="o">|</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="n">start</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
</pre></div>



<a name="165170513"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170513" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170513">(May 08 2019 at 15:00)</a>:</h4>
<p>is Miri-clean</p>



<a name="165170516"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170516" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170516">(May 08 2019 at 15:00)</a>:</h4>
<p>that looks reasonable</p>



<a name="165170524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170524">(May 08 2019 at 15:00)</a>:</h4>
<p>what does Miri say?</p>



<a name="165170556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170556">(May 08 2019 at 15:01)</a>:</h4>
<p><span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span></p>



<a name="165170560"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170560" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170560">(May 08 2019 at 15:01)</a>:</h4>
<p>nothing</p>



<a name="165170591"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170591" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170591">(May 08 2019 at 15:01)</a>:</h4>
<p>Cc <span class="user-mention" data-user-id="137587">@Gankro</span> -- another case of mutable refs being more ergonomic causing trouble</p>



<a name="165170600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170600">(May 08 2019 at 15:01)</a>:</h4>
<p>well actually the code got IMO nicer with raw ptrs but maybe I am weird^^</p>



<a name="165170620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170620">(May 08 2019 at 15:02)</a>:</h4>
<p>Yeah, it's not worse, certainly</p>



<a name="165170725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170725">(May 08 2019 at 15:03)</a>:</h4>
<blockquote>
<p>every time you use data, you are asserting that this is a unique reference</p>
</blockquote>
<p>So because the element was a <code>&amp;mut T</code> and still live, and then I tried to call a <code>fn(&amp;mut [T])</code> that contained that same element, that caused aliasing?</p>



<a name="165170733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170733">(May 08 2019 at 15:03)</a>:</h4>
<p>Very similar to the linked list PR that you submitted and I "reviewed"?</p>



<a name="165170787"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170787" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170787">(May 08 2019 at 15:04)</a>:</h4>
<blockquote>
<p>mutable refs being more ergonomic</p>
</blockquote>
<p>I'd say that my main reason was trying to get out of <code>unsafe</code> as fast as possible.</p>



<a name="165170856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165170856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165170856">(May 08 2019 at 15:04)</a>:</h4>
<blockquote>
<blockquote>
<p>every time you use data, you are asserting that this is a unique reference</p>
</blockquote>
<p>So because the element was a <code>&amp;mut T</code> and still live, and then I tried to call a <code>fn(&amp;mut [T])</code> that contained that same element, that caused aliasing?</p>
</blockquote>
<p>yes</p>



<a name="165171035"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165171035" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165171035">(May 08 2019 at 15:06)</a>:</h4>
<p>Someday I'll get it!</p>



<a name="165171108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165171108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165171108">(May 08 2019 at 15:07)</a>:</h4>
<p>And the iterator-only version <em>didn't</em> cause UB because the <code>&amp;mut T</code> <strong>wasn't</strong> live when we made the subsequent call to <code>get_unchecked_mut</code></p>



<a name="165171143"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165171143" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165171143">(May 08 2019 at 15:07)</a>:</h4>
<p>not sure what that version was, but yes -- if you dont use the invalidated ptr again later, there's no issue</p>



<a name="165174954"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165174954" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165174954">(May 08 2019 at 15:51)</a>:</h4>
<blockquote>
<p>not sure what that version was</p>
</blockquote>
<p><a href="https://play.integer32.com/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=26ae035045e89cd54b72d62413e4f601" target="_blank" title="https://play.integer32.com/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=26ae035045e89cd54b72d62413e4f601">https://play.integer32.com/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=26ae035045e89cd54b72d62413e4f601</a></p>



<a name="165176139"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165176139" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165176139">(May 08 2019 at 16:05)</a>:</h4>
<p>Is there any way for Miri to point at the two (or more?) offending locations? If it pointed at <code>data.get_unchecked_mut(i);</code> I might have had more luck fixing this without consulting The Great Oracle</p>



<a name="165188108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165188108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165188108">(May 08 2019 at 18:27)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> that would be <a href="https://github.com/rust-lang/miri/issues/531" target="_blank" title="https://github.com/rust-lang/miri/issues/531">https://github.com/rust-lang/miri/issues/531</a></p>



<a name="165188184"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165188184" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165188184">(May 08 2019 at 18:28)</a>:</h4>
<p>but I have no idea how to implement that even remotely efficiently. looks like it requires remembering items when they get removed from the stack, and storing a stacktrace of where that happened. for <em>every single item</em>. that's crazy.</p>



<a name="165188519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165188519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165188519">(May 08 2019 at 18:32)</a>:</h4>
<p>"easy" — just run the program backwards once you find the problem :-)</p>



<a name="165191504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165191504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165191504">(May 08 2019 at 19:07)</a>:</h4>
<p>yay rr in miri, the time travelling interpreter :)</p>



<a name="165196055"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165196055" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165196055">(May 08 2019 at 20:04)</a>:</h4>
<p>priroda can do this</p>



<a name="165196074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165196074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165196074">(May 08 2019 at 20:05)</a>:</h4>
<p>(with horrible perf)</p>



<a name="165196161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165196161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165196161">(May 08 2019 at 20:06)</a>:</h4>
<p>uh yea or we just build a tagging system where you figure out the memory location relevant to the error and "just" record all accesses to that memory location</p>



<a name="165196173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Miri%3A%20no%20item%20to%20reborrow%20for%20Unique/near/165196173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Miri.3A.20no.20item.20to.20reborrow.20for.20Unique.html#165196173">(May 08 2019 at 20:06)</a>:</h4>
<p>then show them to the user in reverse order</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>